package com.mew.slots.util;

import com.mew.slots.util.util.ClazzsGetUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

public class Window {
    public static void main(String[] args) {
/*        List<String> games= ClazzsGetUtil.getClassName("com.mew.slots.core.game");*/
        Frame f = new Frame("点就烂:类型，次数，游戏，线数，免费类型，参数，统计图标");//标题
        //设置窗口的大小和位置
        f.setLocation(700, 300);
        f.setSize(580, 600);
        //将窗口显示出来（默认隐藏）
        //f.setResizable(false);

        Panel p=new Panel();
        //向panel中添加两个组件
        JComboBox box=new JComboBox(new String[]{"普通","全部","免费",
                "三只小猪-锁定金狼狗免费",
                "美女与野兽-锁定免费",
                "猪猪大作战-猪免费",
                "快乐小狗-锁定免费"});

        TextField textField = new TextField(5);
        textField.setText("10000");
        TextField gameField = new TextField(4);
        TextField lineField = new TextField(2);
        TextField freeTypeField = new TextField(2);
        freeTypeField.setText("0");
        TextField argsField = new TextField(5);
        TextField sField = new TextField(2);
        Button button = new Button("start");
        p.add(box);
        p.add(textField);
        p.add(gameField);
        p.add(lineField);
        p.add(freeTypeField);
        p.add(argsField);
        p.add(sField);
        p.add(button);//按钮上的文字


        TextArea out = new TextArea(33,73);
        p.add(out);
        //将panel放在frame
        f.add(p);

        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0); //点击右上角关闭按钮,退出虚拟机
            }
        });

        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String text = textField.getText();
                String game = gameField.getText();
                String lineText = lineField.getText();
                String  s = sField.getText();
                String gameType = box.getSelectedItem().toString();
                String freeTypeStr = freeTypeField.getText();
                String  as = argsField.getText();
                int num = canParseInt(text) ? Integer.parseInt(text) : 1000;
                int freeType = canParseInt(freeTypeStr) ? Integer.parseInt(freeTypeStr) : 0;
                int line = canParseInt(lineText) ? Integer.parseInt(lineText) : 0;
                if(null!=as&&!"".equals(as)){
                    String[] argss = as.split(",");;
                    int[] args = new int[argss.length];
                    for (int i = 0; i < argss.length; i++) {
                        args[i] =Integer.parseInt(argss[i]);
                    }
                    App.args = args;
                }
                App.cend = true;
                App.out = out;
                App.end = num;
                App.game = game;
                App.line = line;
                App.s = s;
                App.gameType=gameType;
                App.freeType = freeType;
                try {
                    App.appStart();
                } catch (Exception ex) {
                    out.setText(ex.toString()+":"+ex.getLocalizedMessage());
                    ex.printStackTrace();
                }
            }
        };

        button.addMouseListener(mouseAdapter);

        f.setVisible(true);
    }

    public static boolean canParseInt(String str){
        if(str == null){ //验证是否为空
            return false;
        }

        return str.matches("\\d+"); //使用正则表达式判断该字符串是否为数字，第一个\是转义符，\d+表示匹配1个或 //多个连续数字，"+"和"*"类似，"*"表示0个或多个

    }
}
